package com.xyd.service.impl;

import com.xyd.entity.Cart;
import com.xyd.entity.Order;
import com.xyd.mapper.CartMapper;
import com.xyd.mapper.OrderDetailMapper;
import com.xyd.mapper.OrderMapper;
import com.xyd.mapper.ProductMapper;
import com.xyd.service.OrderService;
import com.xyd.util.MybatisUtil;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/11 16:30
 */
public class OrderServiceImpl implements OrderService {

  private SqlSession sqlSession;

  @Override
  public Order getOrderByUserId(String euUserId) {
    sqlSession = MybatisUtil.getSqlSession();
    Order order = sqlSession.getMapper(OrderMapper.class).getOrderByUserId(euUserId);
    MybatisUtil.closeSqlSession(sqlSession);
    return order;
  }

  @Override
  public List<Order> getAllOrderByPage(Integer start, Integer pageSize) {
    sqlSession = MybatisUtil.getSqlSession();
    List<Order> list = sqlSession.getMapper(OrderMapper.class).getAllOrderByPage(start, pageSize);
    MybatisUtil.closeSqlSession(sqlSession);
    return list;
  }

  @Override
  public int getCountOrder() {
    sqlSession = MybatisUtil.getSqlSession();
    int x = sqlSession.getMapper(OrderMapper.class).getCountOrder();
    MybatisUtil.closeSqlSession(sqlSession);
    return x;
  }

  @Override
  public List<Order> getOrderByIdName(Order order) {
    sqlSession = MybatisUtil.getSqlSession();
    List<Order> list = sqlSession.getMapper(OrderMapper.class).getOrderByIdName(order);
    MybatisUtil.closeSqlSession(sqlSession);
    return list;
  }

  @Override
  public Order getOrderByEoId(long id) {
    sqlSession = MybatisUtil.getSqlSession();
    Order order = sqlSession.getMapper(OrderMapper.class).getOrderByEoId(id);
    MybatisUtil.closeSqlSession(sqlSession);
    return order;
  }


  @Override
  public int changeOrderByEoId(Order order) {
    sqlSession = MybatisUtil.getSqlSession();
    int x = sqlSession.getMapper(OrderMapper.class).changeOrderByEoId(order);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return x;
  }

  @Override
  public int deleteOrderByEoId(long id) {
    sqlSession = MybatisUtil.getSqlSession();
    int x = sqlSession.getMapper(OrderMapper.class).deleteOrderByEoId(id);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return x;
  }

  @Override
  public Order generateOrder(String[] epIds) {
    return null;
  }

  @Override
  public Long addOrder(String euUserId, String euUsername, String euAddress, double eoCost) {
    sqlSession = MybatisUtil.getSqlSession();
    Long eoId = sqlSession.getMapper(OrderMapper.class).addOrder(euUserId, euUsername, euAddress, eoCost);
    List<Cart> carts = sqlSession.getMapper(CartMapper.class).findAllMyCart(euUserId);
    boolean boo = sqlSession.getMapper(OrderDetailMapper.class).addProducts(carts,eoId);
    if (!boo) {
      return null;
    }
    boo = sqlSession.getMapper(CartMapper.class).updateStatus(euUserId);
    if (!boo) {
      return null;
    }
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return eoId;
  }
}
